﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:DlySale
    /// </summary>
    public partial class DlySale
    {
        public DlySale()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from DlySale");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.BigInt)
            };
            parameters[0].Value = DlyOrder;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.DlySale model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into DlySale(");
            strSql.Append("Vchcode,PtypeId,Qty,unit,unit1,qty1,comment,qtytotal,Date)");
            strSql.Append(" values (");
            strSql.Append("@Vchcode,@PtypeId,@Qty,@unit,@unit1,@qty1,@comment,@qtytotal,@Date)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt, 8),
                    new SqlParameter("@PtypeId", SqlDbType.VarChar, 25),
                    new SqlParameter("@Qty", SqlDbType.Int, 4),
                    new SqlParameter("@unit", SqlDbType.Int, 4),
                    new SqlParameter("@unit1", SqlDbType.Int, 4),
                    new SqlParameter("@qty1", SqlDbType.Int, 4),
                    new SqlParameter("@comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@qtytotal", SqlDbType.Int, 4),
                    new SqlParameter("@Date", SqlDbType.DateTime) };
            parameters[0].Value = model.Vchcode;
            parameters[1].Value = model.PtypeId;
            parameters[2].Value = model.Qty;
            parameters[3].Value = model.unit;
            parameters[4].Value = model.unit1;
            parameters[5].Value = model.qty1;
            parameters[6].Value = model.comment;
            parameters[7].Value = model.qtytotal;
            parameters[8].Value = model.Date;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.DlySale model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update DlySale set ");
            strSql.Append("Vchcode=@Vchcode,");
            strSql.Append("PtypeId=@PtypeId,");
            strSql.Append("Qty=@Qty,");
            strSql.Append("unit=@unit,");
            strSql.Append("unit1=@unit1,");
            strSql.Append("qty1=@qty1,");
            strSql.Append("comment=@comment,");
            strSql.Append("qtytotal=@qtytotal,");
            strSql.Append("Date=@Date");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt, 8),
                    new SqlParameter("@PtypeId", SqlDbType.VarChar, 25),
                    new SqlParameter("@Qty", SqlDbType.Int, 4),
                    new SqlParameter("@unit", SqlDbType.Int, 4),
                    new SqlParameter("@unit1", SqlDbType.Int, 4),
                    new SqlParameter("@qty1", SqlDbType.Int, 4),
                    new SqlParameter("@comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@qtytotal", SqlDbType.Int, 4),
                    new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@DlyOrder", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.Vchcode;
            parameters[1].Value = model.PtypeId;
            parameters[2].Value = model.Qty;
            parameters[3].Value = model.unit;
            parameters[4].Value = model.unit1;
            parameters[5].Value = model.qty1;
            parameters[6].Value = model.comment;
            parameters[7].Value = model.qtytotal;
            parameters[8].Value = model.Date;
            parameters[9].Value = model.DlyOrder;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlySale ");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.BigInt)
            };
            parameters[0].Value = DlyOrder;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string DlyOrderlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlySale ");
            strSql.Append(" where DlyOrder in (" + DlyOrderlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlySale GetModel(long DlyOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 DlyOrder,Vchcode,PtypeId,Qty,unit,unit1,qty1,comment,qtytotal,Date from DlySale ");
            strSql.Append(" where DlyOrder=@DlyOrder");
            var parameters = new SqlParameter[] { new SqlParameter("@DlyOrder", SqlDbType.BigInt)
            };
            parameters[0].Value = DlyOrder;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlySale DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.DlySale();
            if (row != null)
            {
                if (row["DlyOrder"] != null && row["DlyOrder"].ToString() != string.Empty)
                {
                    model.DlyOrder = long.Parse(row["DlyOrder"].ToString());
                }
                if (row["Vchcode"] != null && row["Vchcode"].ToString() != string.Empty)
                {
                    model.Vchcode = long.Parse(row["Vchcode"].ToString());
                }
                if (row["PtypeId"] != null)
                {
                    model.PtypeId = row["PtypeId"].ToString();
                }
                if (row["Qty"] != null && row["Qty"].ToString() != string.Empty)
                {
                    model.Qty = int.Parse(row["Qty"].ToString());
                }
                if (row["unit"] != null && row["unit"].ToString() != string.Empty)
                {
                    model.unit = int.Parse(row["unit"].ToString());
                }
                if (row["unit1"] != null && row["unit1"].ToString() != string.Empty)
                {
                    model.unit1 = int.Parse(row["unit1"].ToString());
                }
                if (row["qty1"] != null && row["qty1"].ToString() != string.Empty)
                {
                    model.qty1 = int.Parse(row["qty1"].ToString());
                }
                if (row["comment"] != null)
                {
                    model.comment = row["comment"].ToString();
                }
                if (row["qtytotal"] != null && row["qtytotal"].ToString() != string.Empty)
                {
                    model.qtytotal = int.Parse(row["qtytotal"].ToString());
                }
                if (row["Date"] != null && row["Date"].ToString() != string.Empty)
                {
                    model.Date = DateTime.Parse(row["Date"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select DlyOrder,Vchcode,PtypeId,Qty,unit,unit1,qty1,comment,qtytotal,Date ");
            strSql.Append(" FROM DlySale ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DlyOrder,Vchcode,PtypeId,Qty,unit,unit1,qty1,comment,qtytotal,Date ");
            strSql.Append(" FROM DlySale ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DlySale ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.DlyOrder desc");
            }
            strSql.Append(")AS Row, T.*  from DlySale T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

